from django.conf import settings
from django.core.paginator import Paginator, EmptyPage
from django.http import JsonResponse


# 封装房屋查询中的分页以及返回数据
def house_info(houses,page):
    paginator = Paginator(houses, 3)
    # 获取每页商品数据
    try:
        page_houses = paginator.page(page)
    except EmptyPage:
        return JsonResponse({'errno': 4004, 'errmsg': '数据出错'})
    # 获取列表页总页数
    total_page = paginator.num_pages

    list = []
    for house in page_houses:
        list.append({
            "address": house.address,
            "area_name": house.area.name,
            "ctime": house.create_time,
            "house_id": house.id,
            "img_url": settings.QINIU_BUCKET_URL +'/'+ str(house.index_image_url),
            "order_count": house.order_count,
            "price": house.price,
            "room_count": house.room_count,
            "title": house.title,
            "user_avatar": settings.QINIU_BUCKET_URL +'/'+ str(house.user.avatar)
        })

    # 构建返回数据
    data = {

    }

    return data